# coding:utf-8
# author:frank
# date:2024/12/31
import pygame

pygame.init()

win_w=500
win_h=600
win = pygame.display.set_mode((win_w,win_h))
pygame.display.set_caption("键盘事件")
win.fill('white')
pygame.display.flip()

font = pygame.font.Font("files/Serendity.ttf",30)
x=0
y=100
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        elif event.type == pygame.KEYDOWN:
            print("键盘按下",event.key,chr(event.key))
            text = font.render(chr(event.key),True,'red')
            # 渲染文字并不断修改x坐标
            x+=15
            win.blit(text,(x,y))
            pygame.display.update()
        elif event.type == pygame.KEYUP:
            print("键盘弹起")